<?php
/*
 * Created on May 14, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 *
 */

$this->load->view('admin/tour/link_bar');
?>

<script type="text/javascript" src="{base_url}js/jquery1.6.1.js" ></script>
<script type="text/javascript" src="{base_url}js/ajaxupload.3.5.js" ></script>

<style>
    #upload{
        margin:30px 0px; padding:5px;
        font-weight:bold; font-size:1.3em;
        font-family:Arial, Helvetica, sans-serif;
        text-align:center;
        background:#f2f2f2;
        color:#3366cc;
        border:1px solid #ccc;
        width:150px; height: 20px;
        cursor:pointer !important;
        -moz-border-radius:5px; -webkit-border-radius:5px;
    }
    .darkbg{
        background:#ddd !important;
    }
    #status{
        font-family:Arial; padding:5px;
    }
    div#files{ list-style:none; padding:0; margin:0; }
    div#files div{ padding:5px; margin-bottom:2px; width:120px; float:left; margin-right:5px;}
    div#files div img{ max-width:100px; max-height:100px; }
    .success{ background:#99f099; border:1px solid #339933; }
    .error{ background:#f0c6c3; border:1px solid #cc6622; }
</style>
<script type="text/javascript" >
    function deleteImage(image_id, image_name, tour_id){
        var url_request = "<?php echo site_url('admin/tour/delete_image/'); ?>";
         $.ajax({
            type: "POST",
            url: url_request ,
            data: "tour_id=" + tour_id + "&image_name="+ image_name ,
            dataType: "json",
            success: function(msg){
                 //if(msg.error == false)
                $('#' + image_id).hide(1);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {

                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: Delete image not successfull." + error.empty);
            }
        });
    }

    $(function(){
        var btnUpload=$('#upload');
        var status=$('#status');
        new AjaxUpload(btnUpload, {
            action: '<?php echo site_url();?>admin/tour/do_upload/{id}',
            name: 'uploadfile',
            onSubmit: function(file, ext){
                if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
                    // extension is not allowed
                    status.text('Only JPG, PNG or GIF files are allowed');
                    return false;
                }
                status.text('Uploading...');
            },
        onComplete: function(file, response){
            //On completion clear the status
            status.text('');
            //Add uploaded file to list
            var msg = jQuery.parseJSON(response);

            //alert(msg.filename);
            //return;
            if(response=="error"){
                $('<li></li>').appendTo('#files').text(file).addClass('error');
            }
            else{
                $('<div class ="success" id="'+msg.id+'"></div>').appendTo('#files').html(
                '<img src="{base_url}data/images/tour/'+
                    msg.filename+'" alt="" height=65 width=65 /><br />'+
                    '<a href="javascript:void();" onclick="insert(\''+msg.filename+ '\', \'vi\');">Insert (Vi)</a>' +
                    ' <a href="javascript:void();" onclick="insert(\''+msg.filename+ '\', \'en\');">Insert (En)</a>' +
                    ' <a href="javascript:void();" onclick="setImageDefault(\''+msg.filename+ '\', \'{id}\');">Default</a>' +
                    ' <a href="javascript:void();" onclick="deleteImage('+msg.id+ ',\''+msg.filename+ '\', \'{id}\')">Delete</a>'
                    );
            }
            //else $('<li></li>').appendTo('#files').text(response).addClass('error');
        }
    });

});
</script>


<script type = "text/javascript">

    function setImageDefault(image_name, tour_id){
         var url_request = "<?php echo site_url('admin/tour/set_image/'); ?>";
         $.ajax({
            type: "POST",
            url: url_request ,
            data: "tour_id=" + tour_id + "&image_name="+ image_name ,
            dataType: "json",
            success: function(msg){
                 //if(msg.error == false)
                 //    $('#' + province_id).hide(1);

            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: " + textStatus.toString() + url_request);
            }
        });
    }

    function deleteLocation(tour_id, province_id){
        $('#' + province_id).hide(1);
        //alert("hidden" + province_id);
        //return;
         var url_request = "<?php echo site_url('admin/tour/delete_location/'); ?>" + tour_id +"/"+province_id;
        //this.display = "none";
        $.ajax({
            type: "POST",
            url: url_request ,
            data: "tour_id=" + tour_id + "&province_id="+ province_id ,
            dataType: "json",
            success: function(msg){
                 if(msg.error == false)
                     $('#' + province_id).hide(1);
                 //$('<div style="float:left; width:200px; padding-top:3px"></div>').appendTo('#location').html('<img align="middle" src="{base_url}images/admin/location.gif">'+msg.pname + ' ' + msg.cname + '<img onclick="deleteLocation({id}, ' + province_id + ')" align="top" src="{base_url}images/admin/delete.gif">');
                //document.getElementById('provinces').innerHTML = msg.content;
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: " + textStatus.toString() + url_request);
            }
        });
    }
// Define the entry point
$(document).ready(function()
{
    // The DOM (document object model) is constructed
    // We will initialize and run our plugin here
    $('#ddlCountry').change(function() {

        var ddlCountry = document.getElementById('ddlCountry');
        var country_id = ddlCountry.options[ddlCountry.selectedIndex].value;
        // xajax_change_access(action, id_profile, id_tab, access);
        //alert(action + " " + id_profile + " " + access);
        //alert(country_id);
        var url_request = "<?php echo site_url('admin/country/load_provinces/'); ?>" + country_id;
        $.ajax({
            type: "POST",
            url: url_request ,
            //data: "name=John&location=Boston",
            dataType: "json",
            success: function(msg){
                //alert( "Data Saved: " + msg.content );
                document.getElementById('provinces').innerHTML = msg.content;
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: " + textStatus.toString() + url_request);
            }
        });
        $('#province').html(value)
    });

    $('#add_province').click(function() {
       // alert("url_request");
       // return;

        var ddlProvince = document.getElementById('ddlProvince');
        var province_id = ddlProvince.options[ddlProvince.selectedIndex].value;

        var url_request = "<?php echo site_url( 'admin/tour/add_province/'); ?>" + {id} + "/"  + province_id;
       //alert("url_request");

        $.ajax({
            type: "POST",
            url: url_request ,
            //data: "name=John&location=Boston",
            dataType: "json",
            success: function(msg){
                 if(msg.error == false){
                    $('<div id="'+province_id+'" style="float:left; width:200px; padding-top:3px"></div>').appendTo('#location').html('<img align="middle" src="{base_url}images/admin/location.gif">'+msg.pname + ' ' + msg.cname + '<img onclick="deleteLocation({id}, ' + province_id + ')" align="top" src="{base_url}images/admin/delete.gif">');

                 }
                 else
                    alert('Toin tai roi.');
                //document.getElementById('provinces').innerHTML = msg.content;
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert('Loi roi.');
                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: " + textStatus.toString() + url_request);
            }
        });

    });
   //$('#result').load('http://api.jquery.com/ajaxComplete/');
});
</script>

<form action="{base_url}admin/tour/save" method="post" name="myform">
    <div class="box_function">
        <table width="747" border="0" cellpadding="0" cellspacing="0" class="listproduct" id="listproduct">
            <tr>
                <td colspan="2" style="padding:5px">
                    <h1>
                        {legend_text}
                    </h1>
                    {error_message}

                </td>
            </tr>

            <tr>
                <td style="padding: 5px">
                    <h5>Danh mục:</h5>
                    {ddlCategory}
                </td>
                <td style="padding: 5px">
                    <h5>URL thân thiện:</h5>
                    <input type="text" style="width: 260px; height:20px" class="text" name="txtFriendlyURL" id="txtFriendlyURL" value="{friendly_url}" maxlength="100" />
                </td>
            </tr>

            <tr>
                <td style="padding: 5px">
                    <h5><?php echo $this->lang->line('vi_name') ?></h5>
                    <input type="text" style="width: 260px; height:20px" class="text" name="txtViName" id="txtViName" value="{vi_name}" maxlength="100" />
                </td>
                <td style="padding: 5px">
                    <h5>Mã tour:</h5>
                    <input type="text" style="width: 260px; height:20px" class="text" name="txtCode" id="txtCode" value="{code}" maxlength="100" />
                </td>
            </tr>

            <tr>
                <td style="padding: 5px">
                    <h5><?php echo lang('en_name') ?></h5>
                    <input type="text" style="width: 260px; height:20px" class="text" name="txtEnName" id="txtEnName" value="{en_name}" maxlength="100" />
                </td>
                <td style="padding: 5px">
                    <h5>Giá:</h5>
                    <input type="text" style="width: 260px; height:20px" class="text" name="txtPrice" id="txtPrice" value="{price}" maxlength="100" />
                </td>
            </tr>


            <tr>
                <td style="padding: 5px">
                    <h5>Loại tour:</h5>
                    {ddlType}
                </td>
                <td style="padding: 5px">
                    <h5>Thời gian:</h5>
                    <input type="text" style="width: 260px; height:20px" class="text" name="txtPeriod" id="txtPeriod" value="{period}" maxlength="100" />
                </td>
            </tr>

            <tr>
                <td style="padding: 5px" colspan="2">
                    <h5>Khởi hành:</h5>
                    <input type="text" style="width: 260px; height:20px" class="text" name="txtDeparture" id="txtDeparture" value="{departure}" maxlength="100" />
                </td>
            </tr>

            <tr>
                <td style="padding: 5px" colspan="2">
                    <h5>Giới thiệu ngắn (tiếng Việt):</h5>
                    <textarea rows="5" cols="90" class="text" name="txtViSummary" id="txtViSummary" >{vi_summary}</textarea>
                </td>
            </tr>

            <tr>
                <td style="padding: 5px" colspan="2">
                    <h5>Giới thiệu ngắn (tiếng Anh):</h5>
                    <textarea rows="5" cols="90" class="text" name="txtEnSummary" id="txtEnSummary" >{en_summary}</textarea>
                </td>
            </tr>

            <tr>
                <td style="padding: 5px" colspan="2">
                    <h5>Hình ảnh:</h5>
                    <script>
                    function insert(file, location){
                       // oFormObject = document.forms['myform'];

                        //CKEDITOR.instances['instanceName'].insertHTML('&lt; img src="http://localhost/vntsolution/images/admin/add.gif"/&gt;');

                        //var imgHtml = CKEDITOR.dom.element.createFromHtml("<img align='absmiddle' src='http://localhost/vntsolution/images/admin/add.gif'>");
                        //CKEDITOR.instances.body.insertElement(imgHtml);

                        //oFormObject.elements["txtViDescription"].value = '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">' + oFormObject.elements["txtViDescription"].value;
                        //oFormObject.elements["txtViDescription"].value = '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">';
                        //alert("sdsd");
                        //document.getElementById("listimage").innerHTML =  '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">';
                        //var writer = new CKEDITOR.htmlWriter();
                        //writer.openTag( '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">' );
                        if(location == 'vi')
                            CKEDITOR.instances.txtViDescription.insertHtml("<img align='absmiddle' src='{base_url}data/images/tour/"+file+"'>");
                        else
                        CKEDITOR.instances.txtEnDescription.insertHtml("<img align='absmiddle' src='{base_url}data/images/tour/"+file+"'>");
                    }
                    </script>

                    <div id="files" >
                    {images}
                     <div class ="success" id="{image_id}"> <img src="{base_url}data/images/tour/{vi_file_name}" alt="" /><br />
                         <a href="javascript:void();" onclick="insert('{vi_file_name}', 'vi');">Insert (Vi)</a>
                         <a href="javascript:void();" onclick="insert('{vi_file_name}', 'en');">Insert (En)</a>
                         <a href="javascript:void();" onclick="setImageDefault('{vi_file_name}', {id});">Default</a>
                         <a href="javascript:void();" onclick="deleteImage({image_id},'{vi_file_name}', {id})">Delete</a>
                    </div>
                    {/images}
                    </div>

                    <div style="clear:both"></div>
                    <div id="upload" ><span>Upload File<span></div>
                                <span id="status" ></span>

                                </td>
                                </tr>

                                <tr>
                                    <td style="padding: 5px" colspan="2">
                                        <h5>Nội dung (tiếng Việt):</h5>
                                        <?php echo $this->ckeditor->editor("txtViDescription", $vi_description, $config); ?>
                                    </td>
                                </tr>

                                <tr>
                                    <td style="padding: 5px" colspan="2">
                                        <h5>Nội dung (tiếng Anh):</h5>
                                        <?php echo $this->ckeditor->editor("txtEnDescription", $en_description, $config); ?>
                                    </td>
                                </tr>

                                <tr>
                                    <td style="padding: 5px" colspan="2">
                                        <h5>
                                            <span style="padding-right: 20px">Quốc gia:{ddlCountry}</span>
                                            <span style="padding-right: 20px">Tỉnh/Thành: <span id="provinces">{ddlProvince}</span> <a  href="javascript:void();"><img id="add_province" src="{base_url}images/admin/add.gif"></a> </span>
                                        </h5>
                                        <div id="location">

                                        {provinces}
                                         <div style="float:left; width:200px; padding-top:3px" id="{pid}">
                                             <img align="middle" src="{base_url}images/admin/location.gif">
                                             {pname}-{cname}
                                             <img onclick="deleteLocation({tid}, {pid})" align="top" src="{base_url}images/admin/delete.gif">
                                         </div>
                                        {/provinces}
                                        </div>
                                    </td>
                                </tr>

                                <tr>
                                    <td style="padding: 5px" colspan="2">
                                        <h5>
                                            <span style="padding-right: 150px">{ckBestSell}<?php echo lang('best_sell_product') ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                            <span style="padding-right: 150px">{ckSpecial}<?php echo lang('specials') ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                            <span>{ckActive}<?php echo lang('active') ?></span>
                                        </h5>
                                    </td>
                                </tr>


                                <tr>
                                    <td></td>
                                    <td>
                                        <?php echo lang('indicates_a_required_field'); ?>
                                    </td>
                                </tr>

                                <tr>
                                    <td></td>
                                    <td>
                                        <input type="submit" class="submit button" name="btSave" value="<?php echo lang('save') ?>" />
                                    </td>
                                </tr>
                                </table>
                                </div>
                                <input type="hidden" name="txtID" id="txtID" value="{id}"/>
                                <input type="hidden" name="txtPosition" id="txtPosition" value="{position}"/>
                                <input type="hidden" name="txtPre" id="txtID" value="{id}"/>
                                </form>